package com.tianji.promotion.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponFormDTO;
import com.tianji.promotion.domain.dto.CouponIssueFormDTO;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.vo.CouponDetailVO;
import com.tianji.promotion.domain.vo.CouponPageVO;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 前端控制器
 * </p>
 *
 * @author xzh
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/coupons")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@Api(tags = "优惠券相关接口")
public class CouponController {

    private final ICouponService couponService;

    /**
     * 新增优惠券
     * @param couponFormDTO
     */
    @PostMapping
    @ApiOperation(value = "新增优惠券")
    public void saveCoupon(@RequestBody @Valid CouponFormDTO couponFormDTO){
        couponService.saveCoupon(couponFormDTO);
    }


    /**
     * 优惠券分页查询
     * @param query
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "优惠券分页查询")
    public PageDTO<CouponPageVO> queryCouponPage(CouponQuery query){
        return couponService.queryCouponPage(query);
    }

    /**
     * 发放待发放和暂停中状态的优惠券
     * @param id
     */
    @PutMapping("/{id}/issue")
    @ApiOperation(value = "发放待发放和暂停中状态的优惠券")
    public void issueCouponByStatus(@PathVariable(value = "id") Long id,
                                    @RequestBody @Valid CouponIssueFormDTO formDTO){
        couponService.issueCouponByStatus(id,formDTO);
    }

    /**
     * 编辑优惠券信息
     * @param couponFormDTO
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑优惠券信息")
    public void updateCouponById(@RequestBody @Valid CouponFormDTO couponFormDTO){
        couponService.updateCouponById(couponFormDTO);
    }

    /**
     * 删除优惠券
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除优惠券")
    public boolean deleteCouponAndScope(@PathVariable(value = "id") Long id){
       return couponService.deleteCouponAndScope(id);
    }

    /**
     * 根据id查询优惠券详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询优惠券详细信息")
    public CouponDetailVO getCouponDetail(@PathVariable(value = "id") Long id){
        return couponService.getCouponDetail(id);
    }

    /**
     * 将发放中的优惠券改为暂停发放
     * @param id
     */
    @PutMapping("/{id}/pause")
    @ApiOperation(value = "将发放中的优惠券改为暂停发放")
    public void stopIssueCouponAndExchangeCodeById(@PathVariable(value = "id") Long id){
        couponService.stopIssueCouponAndExchangeCodeById(id);
    }

    /**
     * 向用户展示优惠券详情
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "向用户展示优惠券详情")
    public List<CouponVO> queryIssuingCoupons(){
        return couponService.queryIssuingCoupons();
    }
}
